package upmc.fr.android.rss.persistence;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import upmc.fr.android.rss.RSSCategory;

public class FilePersistence 
{
	private static final String CONFIGURATION_FILENAME = "data";
	
	public static void persist(List<RSSCategory> categories) throws IOException
	{
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		try
		{
			fos = new FileOutputStream(new File(CONFIGURATION_FILENAME));
			oos = new ObjectOutputStream(fos);			
			oos.writeObject(categories);		
			oos.flush();
			fos.flush();			
		}
		finally
		{
			fos.close();
			oos.close();
		}
	}
	
	public static List<RSSCategory> load() throws IOException, ClassNotFoundException
	{
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		List<RSSCategory> result = null;
		try
		{
			fis = new FileInputStream(new File(CONFIGURATION_FILENAME));
			ois = new ObjectInputStream(fis);			
			result = (List<RSSCategory>)ois.readObject();			
		}
		finally
		{
			fis.close();
			ois.close();
		}
		return result;
	}

}
